package jdo.crud;

import java.io.IOException;
import java.io.PrintWriter;

import javax.jdo.Extent;
import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jdo.entity.Tramway;
import jdo.pmf.PMF;

/**
 * Servlet implementation class ListServlet
 */
@SuppressWarnings("serial")
public class ListTramwayServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		response.setContentType("text/html");
		PersistenceManager pm = PMF.getPersistenceManager();
		PrintWriter page = response.getWriter();
		//impression page html avec integration du sign de jquery mobile
		page.print("<!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd>");
		page.print("<html> <head>");
		page.print("<meta charset=utf-8> <meta name=viewport content=width=device-width ,initial-scale=1>");
		page.print("<link rel=stylesheet href=http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css />");
		page.print("<script src=http://code.jquery.com/jquery-1.9.1.min.js></script>");
		page.print("<script src=http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js></script> </head>");
		
		page.print("<body> <div data-role=\"page\" id=\"page1\">");
	    page.print("<div data-role=\"header\">");
	    page.print("<a href=\"index.html\" data-icon=\"home\" data-transition=\"pop \" data-theme=\"b\" >Home</a>");
	    page.print("<h1>Liste Tramway</h1>");
	    page.print("<a href=/administration.jsp data-icon=\"back\" data-transition=\"pop \" data-theme=\"b\" >Retour</a> </div>");
		
	    page.print("<table data-role=\"table\" id=\"table1\" class=\"ui-responsive table-stroke\"> <thead> <tr><th>ID</th><th>Nom</th><th>Coord_y</th><th>Coord_x</th><th>Suppr</th></tr>");
	    page.print("<tbody>");
	    Extent<Tramway> extent = pm.getExtent(Tramway.class, false);
		for (Tramway record : extent) {
			long id_tram = record.getKey();
			page.print("<tr><td>" + id_tram + "</td><td>" + record.getNom() + "</td><td>"
					+ record.getCoordX() + "</td><td>" + record.getCoordY() + "</td><td>"
					+ "<a href=/deleteTramway?tramwayid_tram=" + id_tram + " data-role=\"button\" data-icon=\"delete\" data-mini=\"true\" date-inline=\"true\" data-iconpos=\"notext\">Delete</a></td></tr>");
		}
		page.print("</tbody> </table> </body> </html>");
		pm.close();
	}

}
